package com.teaching.teachingsupport.controller.teachercontroller;

import com.teaching.teachingsupport.common.api.Result;
import com.teaching.teachingsupport.common.pojo.dto.response.CourseResponse;
import com.teaching.teachingsupport.common.pojo.dto.response.SyllabusCreateDTO;
import com.teaching.teachingsupport.common.pojo.dto.response.SyllabusDTO;
import com.teaching.teachingsupport.service.service2.teacherservice.SyllabusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

import static com.teaching.teachingsupport.common.enums.ResultStatusEnum.VALIDATE_FAILED;

@RestController
@RequestMapping("/api/syllabus")
public class SyllabusController {

    @Autowired
    private SyllabusService syllabusService;

    // 获取课程列表
    @GetMapping("/course-list")
    public List<CourseResponse> getCourseList() {
        return syllabusService.getCourseList();
    }

    // 获取教学大纲列表
    @GetMapping("/list")
    public Result<List<SyllabusDTO>> getSyllabusList(
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String courseName) {
        return Result.success(syllabusService.getSyllabusList(status, courseName));
    }

    // 创建教学大纲
    @PostMapping("/createSyllabus")
    public Result<Void> createSyllabus(
            @RequestParam("courseName") String courseName,
            @RequestParam("grades") List<String> grades,
            @RequestParam("file") MultipartFile file) {
        
        // 验证文件格式
        if (!file.getOriginalFilename().endsWith(".md")) {
            return Result.failed(VALIDATE_FAILED, "只支持.md格式文件");
        }

        syllabusService.createSyllabus(SyllabusCreateDTO.builder()
                .courseName(courseName)
                .grades(grades)
                .file(file)
                .build());

        return Result.success();
    }
} 